package com.demo.java.OD51_100.OD59;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【事件推送】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/145601143
 */
public class OdMain2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int m = scanner.nextInt();  // A 集合大小
        int n = scanner.nextInt();  // B 集合大小
        int r = scanner.nextInt();  // 最大允许差值

        int[] numsA = new int[m];
        int[] numsB = new int[n];

        // 读取集合 A
        for (int i = 0; i < m; i++) {
            numsA[i] = scanner.nextInt();
        }

        // 读取集合 B
        for (int i = 0; i < n; i++) {
            numsB[i] = scanner.nextInt();
        }

        for (int ai : numsA) {
            int index = binarySearch(numsB, ai);
            if (index < n && numsB[index] - ai <= r) {
                System.out.println(ai + " " + numsB[index]);
            }
        }
    }

    // 二分查找第一个 >= target 的 B[j]
    private static int binarySearch(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left; // 返回第一个大于等于 target 的索引
    }
}